"""
Case Type   : 堆表多块预读
Case Name   : gs_guc reload 方式设置vacuum_bulk_read_size参数
Create At   : 2024/01/23
Owner       : songjing20
Description :
    1.gs_guc reload 设置参数值
    2.输入错误值
    3.清理环境
Expect      :
    1.成功
    2.报错
    3.成功
History     :
"""
import os
import unittest

from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class MultiBulkRread(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.common = Common()
        self.constant = Constant()
        self.param = 'vacuum_bulk_read_size'
        self.init_value = self.common.show_param(self.param)
        self.values = (1, 32, 64, "'512kB'", "'8kB'", 0)
        self.err_values = (-1, 65, "'8kb'", "''", 111111111111, "'//'", "'#'")


    def test_multi_bulk_read(self):
        text = '-----step1:gs_guc reload 设置参数值;expect:成功-----'
        self.log.info(text)
        for value in self.values:
            self.log.info(f'-----设置为:{value}-----')
            res = self.pri_sh.execute_gsguc(
                'reload', self.constant.GSGUC_SUCCESS_MSG, f"{self.param}={value}")
            self.log.info(res)
            self.assertTrue(res, f'执行失败:{text}')
            self.pri_sh.restart_db_cluster()
            res = self.common.show_param(self.param)
            if isinstance(value, int) or '0kB' in value:
                self.assertTrue(f"{value * 8}kB" in res or '0' == res.strip(),
                                f'执行失败:{text}')
            else:
                self.assertIn(value.strip("'"), res, f'执行失败:{text}')

        text = '-----step2:输入错误值;expect:报错-----'
        self.log.info(text)
        for value in self.err_values:
            self.log.info(f"-----设置为：{value}，报错-----")
            res = self.pri_sh.execute_gsguc(
                'reload', self.constant.GSGUC_SUCCESS_MSG, f"{self.param}={value}")
            self.log.info(res)
            self.assertFalse(res, f'执行失败:{text}')

    def tearDown(self):
        text = '-----step5:清理环境;expect:成功-----'
        self.log.info(text)
        res = self.pri_sh.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG, f"{self.param}={self.init_value}")
        self.log.info(res)
        self.pri_sh.restart_db_cluster()
        val = self.common.show_param(self.param)
        self.assertTrue(res, f'执行失败:{text}')
        self.assertEqual(val, self.init_value, f'执行失败:{text}')
        self.log.info(f"-----{os.path.basename(__file__)} end-----")

